package com.example.star.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.star.R;
import com.example.star.entity.Feedback;
import com.example.star.entity.Message;

import java.util.List;

public class FeedbackAdapter extends BaseAdapter {

    private Context context;
    private List<Feedback> feedbackList;

    public FeedbackAdapter(Context context, List<Feedback> feedbackList) {
        this.context = context;
        this.feedbackList = feedbackList;
    }

    @Override
    public int getCount() {
        return feedbackList.size();
    }

    @Override
    public Object getItem(int position) {
        return feedbackList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        if (convertView == null) {
            view = LayoutInflater.from(context).inflate(R.layout.item_feedback, parent, false);
        } else {
            view = convertView;
        }

        Feedback feedback = feedbackList.get(position);
        ImageView avatarImage = view.findViewById(R.id.avatar_image_view);
        TextView usernameTextView = view.findViewById(R.id.username_text_view1);
        TextView messageTextView = view.findViewById(R.id.message_text_view);

        // 显示用户名
        String userName = feedback.getUserName();
        Log.d("FeedbackAdapter", "User Name: " + userName);
        if (userName != null && !userName.isEmpty()) {
            usernameTextView.setText(userName);
        } else {
            usernameTextView.setText("未知用户名");
        }

        // 显示消息内容
        messageTextView.setText(feedback.getMessage());

        // 显示用户头像
        if (feedback.getUser() != null && feedback.getUser().getAvatar() != null) {
            Glide.with(context)
                    .load(feedback.getUser().getAvatar())
                    .into(avatarImage);
        }
        return view;
    }
}